#include "stdafx.h"
#include "puParticleEditor.h"

using namespace Ogitors;

static CPuParticleEditorFactory ParticleEditorFactory;

//-------------------------------------------------------------------------------
CPuParticleEditor::CPuParticleEditor(Ogre::String typestring, EDITORTYPE etype) : CNodeEditor(typestring, etype)
{
    m_ParticleHandle = 0;
    m_ParticleSystem = "E_dance_p_lv2";//
    m_UsesGizmos = true;
    m_UsesHelper = false;
    m_EditFlags = CAN_MOVE | CAN_SCALE | CAN_ROTATE | CAN_CLONE | CAN_DELETE  | CAN_FOCUS | CAN_DRAG | CAN_UNDO;
}
//-------------------------------------------------------------------------------
CBaseEditorFactory *CPuParticleEditor::GetFactory()
{
    return &ParticleEditorFactory;
}
//-------------------------------------------------------------------------------
CBaseEditorFactory *CPuParticleEditor::GetFactoryDynamic()
{
    return &ParticleEditorFactory;
}
//-------------------------------------------------------------------------------
void CPuParticleEditor::SetHandle(void *objHandle,bool display)
{
    if(m_Handle)
	    UnRegisterObjectName();
    m_ParticleHandle = (ParticleUniverse::ParticleSystem *)objHandle;
    m_Handle = m_ParticleHandle->getParentSceneNode();
    m_Name = m_ParticleHandle->getName();
  
    RegisterObjectName();
    if(display) SetupPropertiesWnd();
}

// void	CPuParticleEditor::SetNodeHandle(void *objHandle,Ogre::String & name ,bool display)
// 	{
// 		if(m_Handle)
// 			UnRegisterObjectName();
// 		m_ParticleHandle = NULL;
// 		m_Handle = objHandle;
// 		m_Name = name;
// 
// 		RegisterObjectName();
// 		if(display) SetupPropertiesWnd();
// 	}

//-------------------------------------------------------------------------------
void CPuParticleEditor::FreeHandle()
{
    CBaseEditor::FreeHandle();
    if(m_ParticleHandle)
    {
        UnRegisterObjectName();
        m_Name = "";
        DETACHFROMPARENT(m_ParticleHandle);

ParticleUniverse::ParticleSystemManager::getSingletonPtr()->destroyParticleSystem(m_ParticleHandle, m_ParticleHandle->_getManager());

	   // m_ParticleHandle->_getManager()->destroyParticleSystem(m_ParticleHandle);
        m_ParticleHandle = 0;
    }
    if(m_Handle)
    {
        m_Handle->getParentSceneNode()->removeAndDestroyChild(m_Handle->getName());
        m_Handle = 0;
    }

	if (m_ParticleHandle)
	{
		ParticleUniverse::ParticleSystemManager::getSingletonPtr()->destroyParticleSystem(m_ParticleHandle, m_ParticleHandle->_getManager());
		m_ParticleHandle = NULL;
	}

}
//-----------------------------------------------------------------------------------------
void CPuParticleEditor::ShowBoundingBox(bool bShow) 
{
    if(!m_BoxParentNode && m_EditorType != ETYPE_BASE)
        CreateBoundingBox();

    m_BBoxNode->setVisible(bShow);
}//-------------------------------------------------------------------------------
Ogre::AxisAlignedBox CPuParticleEditor::GetAABB() 
{
    Ogre::AxisAlignedBox box = m_ParticleHandle->getBoundingBox();
    box.scale(GetScale());
    return box;
}
    Ogre::StringVector gParticleList;
//-------------------------------------------------------------------------------
bool CPuParticleEditor::SetupPropertiesWnd()
{
    if(!CNodeEditor::SetupPropertiesWnd()) 
        return false;

   // Ogre::StringVector ParticleList;

	 DETACHFROMPARENT(m_ParticleHandle);

	 if (gParticleList.size() <= 0)
	 {
		 ParticleUniverse::ParticleSystemManager::getSingletonPtr()->particleSystemTemplateNames(gParticleList);
	 }


    m_System->FillPropertyOptions(this,BIND_MESHFILE,gParticleList);
    m_System->SetProperty(this,BIND_MESHFILE,GetMeshFileName());
    m_System->SetProperty(this,BIND_CASTSHADOWS,m_ParticleHandle->getCastShadows());

    Ogre::Vector3 vAABB = GetAABB().getSize();
    m_System->SetProperty(this,BIND_AABB,vAABB);

    return true;
}

	 void	CPuParticleEditor::SetMeshFileName(int BIND_ID,Ogre::String val,bool refresh) 
	{
		Ogre::StringVector::iterator it = find(gParticleList.begin(),gParticleList.end(),val);
		if (it == gParticleList.end())
		{
			return;
		}

		if (val == m_ParticleSystem)
		{
			return ;
		}
	 
		m_Handle->detachObject(m_ParticleHandle);
		ParticleUniverse::ParticleSystemManager::getSingletonPtr()->destroyParticleSystem(m_ParticleHandle, m_ParticleHandle->_getManager());
		m_ParticleHandle = ParticleUniverse::ParticleSystemManager::getSingletonPtr()->createParticleSystem(GetName(),val,OgitorsRoot::getSingletonPtr()->GetSceneManager());

		m_Handle->attachObject(m_ParticleHandle);

		// 		m_Handle->setVisible(true);
		// 		m_ParticleHandle->setVisible(true);

		m_ParticleHandle->start();

		m_ParticleSystem = val;
	}
// <PuParticle name="Entity11" hidden="false" particle="EmptyParticle" >
// <position x="0"  y="0"  z="0"/>
// <rotation x="0"  y="0"  z="0"  w="-1"/>
// <scale x="1"  y="1"  z="1"/>
// </PuParticle >
 
void    CPuParticleEditor::GetObjectUserData(std::ofstream &outfile)
{
 
	Vector3    pos = GetPosition();
	Quaternion qua = GetOrientation();
	Vector3    sca = GetScale();

	outfile << "<puparticle name= " <<"\"" << GetName() <<"\"" <<  " particle= " <<"\"" <<m_ParticleSystem<<"\"" << ">\n";

	outfile<<"<position x=" <<"\"" <<pos.x <<"\""
		<<" y="<<"\"" <<pos.y<<"\"" 
		<<" z="<<"\"" <<pos.z<<"\""<<"/>\n";

	outfile<<"<rotation x=" <<"\"" <<qua.x <<"\""
		<<" y="<<"\"" <<qua.y<<"\"" 
		<<" z="<<"\"" <<qua.z<<"\""
		<<" w="<<"\"" <<qua.w<<"\""<<"/>\n";

	outfile<<"<scale x=" <<"\"" <<sca.x <<"\""
		<<" y="<<"\"" <<sca.y<<"\"" 
		<<" z="<<"\"" <<sca.z<<"\""<<"/>\n";
}


//-------------------------------------------------------------------------------
void CPuParticleEditor::GetObjectProperties(Ogre::NameValuePairList &retList)
{
    retList.clear();
    CNodeEditor::GetObjectProperties(retList);
    retList["particle"] = m_ParticleSystem;
}
//-------------------------------------------------------------------------------
bool CPuParticleEditor::HandleCustomFunction(int BIND_ID,Ogre::String val)
{
  return true;
}
//-------------------------------------------------------------------------------
CBaseEditor *CPuParticleEditor::Restore(Ogre::NameValuePairList &retList)
{
    Ogre::NameValuePairList theList;
    GetObjectProperties(theList);
    Ogre::NameValuePairList::const_iterator ni;

    CPuParticleEditor *object = static_cast<CPuParticleEditor*>(CNodeEditor::Restore(retList));
    retList = theList;
    return object;
}
//-----------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------
//--------------CPARTICLEEDITORFACTORY-------------------------------------------
//-------------------------------------------------------------------------------
CBaseEditor *CPuParticleEditorFactory::CreateObject(CBaseEditor **parent, Ogre::NameValuePairList &params)
{
    Ogre::SceneNode* pNode;
    ParticleUniverse::ParticleSystem* pParticle;
    Ogre::String tail;
    Ogre::String objName = params["name"];

    Ogre::NameValuePairList::const_iterator ni;
    if((ni = params.find("particle")) == params.end()) 
        params["particle"] = "ed_0001_1";

    OgitorsRoot *ogRoot = OgitorsRoot::getSingletonPtr();
    CPuParticleEditor *object = new CPuParticleEditor();

    if ((ni = params.find("init")) != params.end())
    {
        tail = ogRoot->CreateUniqueID("Particle","Node");
	    objName = "Particle" + tail;
    }

    Ogre::Vector3 vPos(0,0,0);
    Ogre::Quaternion qOrient = Ogre::Quaternion::IDENTITY;

    if ((ni = params.find("position")) != params.end())
    {
	    vPos = Ogre::StringConverter::parseVector3(ni->second);
    }

    if ((ni = params.find("orientation")) != params.end())
    {
	    qOrient = Ogre::StringConverter::parseQuaternion(ni->second);
    }

    pNode = (*parent)->GetNode()->createChildSceneNode(objName,vPos,qOrient);
	//if (params["particle"] != "")
	{
		pParticle = ParticleUniverse::ParticleSystemManager::getSingletonPtr()->createParticleSystem(objName,params["particle"],ogRoot->GetSceneManager());
		pNode->attachObject(pParticle);
		pParticle->setKeepLocal(true);
		pParticle->start();
		
	}


    object->SetHandle(pParticle,false);
    object->m_ParticleSystem = params["particle"];

    if ((ni = params.find("scale")) != params.end())
    {
	    pNode->setScale(Ogre::StringConverter::parseVector3(ni->second));
    }

    if ((ni = params.find("autotracktarget")) != params.end())
    {
        object->m_AutoTrackTarget = ni->second;
    }

    if ((ni = params.find("locked")) != params.end())
    {
        object->SetLocked(Ogre::StringConverter::parseBool(ni->second), false);
    }

    m_InstanceCount++;
    return object;
}
//-------------------------------------------------------------------------------
